﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;


namespace OurSite.Business.Repositories
{
    public interface IUserRepository
    {
        User GetByUserName(string name, string password);
    }

    public class UserRepository : BaseRepository, IUserRepository
    {
        public User GetByUserName(string name, string password)
        {
            using (var tx = session.BeginTransaction())
            {
                User user = session
                    .CreateCriteria<User>()
                    .Add(Restrictions.Eq("UserName", name))
                    .Add(Restrictions.Eq("Password", password))
                    .UniqueResult<User>();

                tx.Commit();
                return user;
            }
        }

    }

    public interface IRoleRepository
    {
        Role GetByRoleName(string name);
    }

    public class RoleRepository : BaseRepository, IRoleRepository
    {
        public Role GetByRoleName(string name)
        {
            using (var tx = session.BeginTransaction())
            {
                Role role = session
                    .CreateCriteria<Role>()
                    .Add(Restrictions.Eq("RoleName", name))
                    .UniqueResult<Role>();

                tx.Commit();
                return role;
            }
        }

    }
}
